ajaynagotha commited on
Commit
ace2b2b
·
verified ·
1 Parent(s): b7473ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -21
app.py CHANGED
@@ -4,6 +4,9 @@ from transformers import AutoTokenizer, AutoModelForQuestionAnswering
4
  import torch
5
  import logging
6
  import sys
 
 
 
7
 
8
  # Set up logging
9
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -41,9 +44,8 @@ def clean_answer(answer):
41
  cleaned_answer = ' '.join(token for token in answer.split() if token not in special_tokens)
42
  return cleaned_answer.strip()
43
 
44
- def answer_question(question, system_prompt, temperature, max_new_tokens, top_p, frequency_penalty, presence_penalty, top_k, echo, best_of):
45
  logger.info(f"Received question: {question}")
46
- logger.info(f"Parameters: temp={temperature}, max_tokens={max_new_tokens}, top_p={top_p}, freq_penalty={frequency_penalty}, pres_penalty={presence_penalty}, top_k={top_k}, echo={echo}, best_of={best_of}")
47
 
48
  try:
49
  logger.info("Combining text from dataset")
@@ -69,36 +71,69 @@ def answer_question(question, system_prompt, temperature, max_new_tokens, top_p,
69
  logger.warning("Generated answer was empty after cleaning")
70
  answer = "I'm sorry, but I couldn't find a specific answer to that question based on the Bhagavad Gita. Could you please rephrase your question or ask about one of the core concepts like dharma, karma, bhakti, or the different types of yoga discussed in the Gita?"
71
 
72
- disclaimer = "\n\nPlease note: This response is generated by an AI model based on the Bhagavad Gita. For authoritative information, please consult the original text or scholarly sources."
73
- full_response = answer + disclaimer
74
  logger.info("Answer generated successfully")
75
 
76
- return full_response
77
 
78
  except Exception as e:
79
  logger.error(f"Error in answer_question function: {str(e)}")
80
  return "I'm sorry, but an error occurred while processing your question. Please try again later."
81
 
82
- logger.info("Setting up Gradio interface")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  iface = gr.Interface(
84
  fn=answer_question,
85
- inputs=[
86
- gr.Textbox(lines=2, placeholder="Enter your question here..."),
87
- gr.Textbox(lines=2, placeholder="System prompt (optional)"),
88
- gr.Slider(minimum=0, maximum=1, step=0.1, value=0.7, label="Temperature"),
89
- gr.Slider(minimum=1, maximum=500, step=1, value=250, label="Max new tokens"),
90
- gr.Slider(minimum=0, maximum=1, step=0.05, value=0.95, label="Top p"),
91
- gr.Slider(minimum=0, maximum=2, step=0.1, value=1, label="Frequency penalty"),
92
- gr.Slider(minimum=0, maximum=2, step=0.1, value=1, label="Presence penalty"),
93
- gr.Slider(minimum=1, maximum=100, step=1, value=1, label="Top k"),
94
- gr.Checkbox(label="Echo"),
95
- gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Best of")
96
- ],
97
  outputs="text",
98
  title="Bhagavad Gita Q&A",
99
  description="Ask a question about the Bhagavad Gita, and get an answer based on the dataset."
100
  )
101
 
102
- logger.info("Launching the Gradio app")
103
- iface.launch()
104
- logger.info("Gradio app launched successfully")
 
 
 
 
 
 
 
 
 
 
 
4
  import torch
5
  import logging
6
  import sys
7
+ from fastapi import FastAPI, HTTPException
8
+ from pydantic import BaseModel
9
+ from fastapi.middleware.cors import CORSMiddleware
10
 
11
  # Set up logging
12
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
44
  cleaned_answer = ' '.join(token for token in answer.split() if token not in special_tokens)
45
  return cleaned_answer.strip()
46
 
47
+ def answer_question(question):
48
  logger.info(f"Received question: {question}")
 
49
 
50
  try:
51
  logger.info("Combining text from dataset")
 
71
  logger.warning("Generated answer was empty after cleaning")
72
  answer = "I'm sorry, but I couldn't find a specific answer to that question based on the Bhagavad Gita. Could you please rephrase your question or ask about one of the core concepts like dharma, karma, bhakti, or the different types of yoga discussed in the Gita?"
73
 
 
 
74
  logger.info("Answer generated successfully")
75
 
76
+ return answer
77
 
78
  except Exception as e:
79
  logger.error(f"Error in answer_question function: {str(e)}")
80
  return "I'm sorry, but an error occurred while processing your question. Please try again later."
81
 
82
+ # FastAPI setup
83
+ app = FastAPI()
84
+
85
+ # Add CORS middleware
86
+ app.add_middleware(
87
+ CORSMiddleware,
88
+ allow_origins=["*"], # Allows all origins
89
+ allow_credentials=True,
90
+ allow_methods=["*"], # Allows all methods
91
+ allow_headers=["*"], # Allows all headers
92
+ )
93
+
94
+ class Question(BaseModel):
95
+ messages: list
96
+
97
+ @app.post("/predict")
98
+ async def predict(question: Question):
99
+ try:
100
+ last_user_message = next((msg for msg in reversed(question.messages) if msg['role'] == 'user'), None)
101
+
102
+ if not last_user_message:
103
+ raise HTTPException(status_code=400, detail="No user message found")
104
+
105
+ user_question = last_user_message['content']
106
+
107
+ answer = answer_question(user_question)
108
+
109
+ disclaimer = "\n\nPlease note: This response is generated by an AI model based on the Bhagavad Gita. For authoritative information, please consult the original text or scholarly sources."
110
+ full_response = answer + disclaimer
111
+
112
+ return {"response": full_response, "isTruncated": False}
113
+
114
+ except Exception as e:
115
+ logger.error(f"Error in predict function: {str(e)}")
116
+ raise HTTPException(status_code=500, detail=str(e))
117
+
118
+ # Gradio interface (optional, for testing)
119
  iface = gr.Interface(
120
  fn=answer_question,
121
+ inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
 
 
 
 
 
 
 
 
 
 
 
122
  outputs="text",
123
  title="Bhagavad Gita Q&A",
124
  description="Ask a question about the Bhagavad Gita, and get an answer based on the dataset."
125
  )
126
 
127
+ # Run both FastAPI and Gradio
128
+ if __name__ == "__main__":
129
+ import uvicorn
130
+ import threading
131
+ import nest_asyncio
132
+
133
+ nest_asyncio.apply()
134
+
135
+ def run_fastapi():
136
+ uvicorn.run(app, host="0.0.0.0", port=7860)
137
+
138
+ threading.Thread(target=run_fastapi, daemon=True).start()
139
+ iface.launch()