0% found this document useful (0 votes)
9 views3 pages

Shakespearean AI Response Generator

This document outlines a Flask web application that utilizes the Hugging Face Inference API to generate responses in a Shakespearean style. It includes functionality for handling user questions, generating AI responses using various models, and providing fallback answers when necessary. The application also features debugging routes to test token validity and model responses.

Uploaded by

anna.ss.0991.z
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Shakespearean AI Response Generator

This document outlines a Flask web application that utilizes the Hugging Face Inference API to generate responses in a Shakespearean style. It includes functionality for handling user questions, generating AI responses using various models, and providing fallback answers when necessary. The application also features debugging routes to test token validity and model responses.

Uploaded by

anna.ss.0991.z
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

from flask import Flask, render_template, request

from huggingface_hub import InferenceClient


import random
import logging

[Link](level=[Link])
logger = [Link](__name__)

# Ваш токен с правами [Link]


client = InferenceClient(token="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")
app = Flask(__name__)

# Префиксы для шекспировского стиля


SHAKESPEARE_PREFIXES = [
"Verily, ",
"Forsooth, ",
"Methinks ",
"Prithee know: ",
"Hark! ",
"By my troth, "
]

# Фоллбэк-ответы
SH_FALLBACK = [
"Verily, thou shalt find thy answer in due time.",
"Forsooth, the fates are clouded in mystery.",
"Methinks the answer lies within thy heart.",
"Prithee, ask again when the moon doth rise.",
"Hark! The spirits whisper 'tis uncertain.",
"By my troth, fortune favors thee not this day.",
"Alack, the crystal ball grows dim.",
"Marry, thou dost ask what cannot be known.",
"Zounds! The answer eludes even the wisest sage.",
"Fie! Ask thee a simpler question, good sir."
]

def shakespeare_prefix(text: str) -> str:


"""Добавляет шекспировский префикс, если в тексте нет характерных слов."""
text = [Link]()
low = [Link]()
if any(w in low for w in ['thou', 'thee', 'thy', 'art', 'doth', 'verily',
'forsooth']):
return text
return [Link](SHAKESPEARE_PREFIXES) + [Link]()

def get_ai_response(question: str) -> str:


# 1) Chat-модели через chat_completion
for model in ["HuggingFaceH4/zephyr-7b-beta", "microsoft/DialoGPT-medium"]:
try:
[Link](f"Chat-completion on {model}")
resp = client.chat_completion(
model=model,
messages=[{"role": "user", "content": question}],
max_tokens=30,
temperature=0.7
)
content = [Link][0].[Link]
return shakespeare_prefix(content)
except Exception as e:
[Link](f"{model} chat_completion failed: {e}")

# 2) GPT-2 и DistilGPT-2 через text_generation


for model in ["gpt2", "distilgpt2"]:
try:
[Link](f"Text-generation on {model}")
resp = client.text_generation(
model=model,
prompt=f"Answer like Shakespeare: {question}",
max_new_tokens=30,
temperature=0.7
)
# Если вернулся генератор, берём первый элемент
if hasattr(resp, "__iter__") and not isinstance(resp, (str, dict)):
try:
resp = next(iter(resp))
except StopIteration:
[Link](f"{model} returned empty generator")
continue

# Достаём текст
if isinstance(resp, dict) and "generated_text" in resp:
text = resp["generated_text"]
elif hasattr(resp, "generated_text"):
text = resp.generated_text
else:
text = str(resp)

return shakespeare_prefix(text)
except Exception as e:
[Link](f"{model} text_generation failed: {e}")

# 3) Если всё упало — фоллбэк


[Link]("Все модели не сработали — возвращаю фоллбэк")
return [Link](SH_FALLBACK)

@[Link]("/", methods=["GET", "POST"])


def magic_ball():
question = ""
answer = ""
error_message = ""
if [Link] == "POST":
question = [Link]("question", "").strip()
if question:
try:
answer = get_ai_response(question)
except Exception as e:
error_message = f"Error generating response: {e}"
[Link](error_message)
return render_template("[Link]",
question=question,
answer=answer,
error_message=error_message)

@[Link]("/debug")
def debug_models():
results = []

# Проверка токена
token_info = "🔑 Token status: "
if [Link] and [Link]("hf_"):
token_info += "✅ OK"
else:
token_info += "❌ Invalid or missing"
[Link](token_info)
[Link]("-" * 50)

# Тест text_generation
for model in ["gpt2", "distilgpt2"]:
try:
r = client.text_generation(model=model, prompt="Hello",
max_new_tokens=5, temperature=0.5)
if hasattr(r, "__iter__"):
r = next(iter(r))
txt = [Link]("generated_text", getattr(r, "generated_text", str(r)))
[Link](f"✅ {model}: {txt}")
except Exception as e:
[Link](f"❌ {model}: {type(e).__name__} {e}")

# Тест chat_completion
for model in ["HuggingFaceH4/zephyr-7b-beta", "microsoft/DialoGPT-medium"]:
try:
r = client.chat_completion(
model=model,
messages=[{"role": "user", "content": "Hello"}],
max_tokens=5,
temperature=0.5
)
txt = [Link][0].[Link]()
[Link](f"✅ {model} chat: {txt}")
except Exception as e:
[Link](f"❌ {model} chat: {type(e).__name__} {e}")

return "<br>".join(results)

@[Link]("/test-token")
def test_token():
from huggingface_hub import whoami
try:
info = whoami(token=[Link])
return f"✅ Token valid: {info['name']}"
except Exception as e:
return f"❌ Token invalid: {type(e).__name__}: {e}"

if __name__ == "__main__":
if not [Link] or [Link]("hf_YOUR_NEW_TOKEN_HERE"):
print("⚠️ WARNING: set your HF token with [Link] scope!")
[Link](debug=True, host="[Link]", port=5000)

You might also like