Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,19 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
# Load .env file
|
| 7 |
load_dotenv()
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
# Initialize Hugging Face client
|
| 11 |
MODEL = "google/flan-t5-small"
|
| 12 |
client = InferenceClient(model=MODEL, token=HF_TOKEN)
|
|
@@ -16,18 +23,27 @@ def ask_question(question: str) -> str:
|
|
| 16 |
if not question.strip():
|
| 17 |
return "Please ask something."
|
| 18 |
try:
|
| 19 |
-
# For FLAN-T5, we need to format prompt as a Q&A
|
| 20 |
prompt = f"Question: {question}\nAnswer:"
|
| 21 |
resp = client.text_generation(
|
| 22 |
prompt=prompt,
|
| 23 |
max_new_tokens=100,
|
| 24 |
temperature=0.7,
|
| 25 |
)
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
|
|
|
| 28 |
return f"Error: {str(e)}"
|
| 29 |
|
| 30 |
-
#
|
| 31 |
iface = gr.Interface(
|
| 32 |
fn=ask_question,
|
| 33 |
inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
|
|
@@ -37,4 +53,5 @@ iface = gr.Interface(
|
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import logging
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# Enable logging for debugging
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
+
|
| 10 |
# Load .env file
|
| 11 |
load_dotenv()
|
| 12 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 13 |
|
| 14 |
+
if not HF_TOKEN:
|
| 15 |
+
raise ValueError("HF_TOKEN not found in .env file!")
|
| 16 |
+
|
| 17 |
# Initialize Hugging Face client
|
| 18 |
MODEL = "google/flan-t5-small"
|
| 19 |
client = InferenceClient(model=MODEL, token=HF_TOKEN)
|
|
|
|
| 23 |
if not question.strip():
|
| 24 |
return "Please ask something."
|
| 25 |
try:
|
|
|
|
| 26 |
prompt = f"Question: {question}\nAnswer:"
|
| 27 |
resp = client.text_generation(
|
| 28 |
prompt=prompt,
|
| 29 |
max_new_tokens=100,
|
| 30 |
temperature=0.7,
|
| 31 |
)
|
| 32 |
+
logging.info(f"HF response: {resp}") # Debug log
|
| 33 |
+
|
| 34 |
+
# Safely extract generated text
|
| 35 |
+
if isinstance(resp, dict) and "generated_text" in resp:
|
| 36 |
+
return resp["generated_text"]
|
| 37 |
+
elif isinstance(resp, list) and len(resp) > 0 and "generated_text" in resp[0]:
|
| 38 |
+
return resp[0]["generated_text"]
|
| 39 |
+
else:
|
| 40 |
+
# Fallback: convert response to string
|
| 41 |
+
return str(resp)
|
| 42 |
except Exception as e:
|
| 43 |
+
logging.error(f"Error in ask_question: {str(e)}")
|
| 44 |
return f"Error: {str(e)}"
|
| 45 |
|
| 46 |
+
# Gradio Web UI
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=ask_question,
|
| 49 |
inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
+
# Serve on all interfaces for Docker/remote access, enable debug logs
|
| 57 |
+
iface.launch(server_name="0.0.0.0", server_port=7860, debug=True)
|